using System;
using System.Collections;
using System.Windows.Forms;

namespace ShadeTree.Binding
{
    public interface IScreenState
    {
        //bool IsControlHidden(object control);
        bool IsControlEnabled(object control);
    }

    public class NulloScreenState : IScreenState
    {
        #region IScreenState Members

        public bool IsControlEnabled(object control)
        {
            return true;
        }

        #endregion
    }

    public class ControlSet
    {
        private readonly ArrayList _all = new ArrayList();

        public int Count
        {
            get { return _all.Count; }
        }

        public Control[] AllControls
        {
            get { return (Control[]) _all.ToArray(typeof (Control)); }
        }

        public void Add(object control)
        {
            if (!_all.Contains(control))
            {
                _all.Add(control);
            }
        }

        public bool Contains(object control)
        {
            return _all.Contains(control);
        }

        public void AddRange(object[] controls)
        {
            foreach (object control in controls)
            {
                Add(control);
            }
        }
    }

    public class ReadOnlyScreenState : IScreenState
    {
        #region IScreenState Members

        public bool IsControlEnabled(object control)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class NormalScreenState : IScreenState
    {
        private readonly ControlSet _all;
        private readonly ControlSet _internalSet = new ControlSet();

        public NormalScreenState(ControlSet all)
        {
            _all = all;
            _internalSet = new ControlSet();
        }

        #region IScreenState Members

        public bool IsControlEnabled(object control)
        {
            if (partOfThisState(control))
            {
                return true;
            }

            return !_all.Contains(control);
        }

        #endregion

        private bool partOfThisState(object control)
        {
            return _internalSet.Contains(control);
        }

        public void Enable(params object[] controls)
        {
            _internalSet.AddRange(controls);
        }
    }

    public class DisableAllButScreenState : IScreenState
    {
        private readonly ControlSet _internalSet = new ControlSet();

        #region IScreenState Members

        public bool IsControlEnabled(object control)
        {
            return _internalSet.Contains(control);
        }

        #endregion

        public void Enable(params object[] controls)
        {
            _internalSet.AddRange(controls);
        }
    }

    public class EnableAllScreenState : IScreenState
    {
        #region IScreenState Members

        public bool IsControlEnabled(object control)
        {
            return true;
        }

        #endregion
    }

    public class EnableAllButScreenState : IScreenState
    {
        private readonly ControlSet _internalSet = new ControlSet();

        #region IScreenState Members

        public bool IsControlEnabled(object control)
        {
            return !_internalSet.Contains(control);
        }

        #endregion

        public void Disable(object[] controls)
        {
            _internalSet.AddRange(controls);
        }
    }
}